#include "ftp.h"

int get_cmd_type(char *data){
	
	if(!strcmp(data,"ls"))   return LS;
	if(!strcmp(data,"pwd"))   return PWD;
	if(!strcmp(data,"quit"))   return QUIT;

	if(!strcmp(data,"lls"))     return LLS;
	if(strstr(data,"lcd") != NULL) return LCD;
	if(strstr(data,"cd") != NULL) return CD;

	if(strstr(data,"get") != NULL) return GET;
	if(strstr(data,"put") != NULL) return PUT;


	return -1;

}


char *GetDir(char *data){

	char *p;
	p = strtok(data," ");
	p = strtok(NULL," ");

	return p;
}

int msg_handler(struct Msg msg,int fd){
	
	int ret,filefd;
	char buf[1024];
	char *dir;

	ret = get_cmd_type(msg.data);
	switch(ret){

		case LS:
		case PWD:
		case CD:
			write(fd,&msg,sizeof(msg));
			break;
		
		case QUIT:
			printf("client quit!\n");
			close(fd);
			exit(-1);
			break;
		case PUT:
			strcpy(buf,msg.data);
			filefd = open(GetDir(buf),O_RDWR);
            if(filefd == -1){
                printf("No this file!\n");
				close(filefd);
                break;
            }
            else if(filefd > 0){
                int n_read = read(filefd,msg.context,sizeof(msg.context));
                close(filefd);
                printf("client read %d characters\n",n_read);
                write(fd,&msg,sizeof(struct Msg));
            }
			break;
		case GET:
            write(fd,&msg,sizeof(msg));
			break;
		case LLS:
			system("ls");
			break;
		case LCD:{
			char *dir = GetDir(msg.data);
			chdir(dir);
			break;		
		}
	}
	return ret;
}

void server_msg_handler(int ret,struct Msg msg,int fd){

	int n_read;
	struct Msg GetMsg;
	int filefd;

	n_read = read(fd,&GetMsg,sizeof(GetMsg));
	if(n_read == 0){
		printf("server is out,qiut...\n");
		exit(-1);
	}	
	else if(ret == GET){
		char *p = GetDir(msg.data);
		filefd = open(p,O_RDWR|O_CREAT,0600);
		write(filefd,GetMsg.data,strlen(GetMsg.data));
		printf(">");
		fflush(stdout);
	}
	else{
		printf("--------------------------------\n");
		printf("\n%s\n",GetMsg.data);
		printf("--------------------------------\n");

		putchar('>');
		fflush(stdout);
	}
}

int main(int argc,char **argv){

	int ret;
	int sfd;
	int cfd;
	socklen_t c_addr_len;
	pid_t pid;
	struct sockaddr_in s_addr;
	struct sockaddr_in c_addr;
	struct Msg msg;

	if(argc != 3){
		
		perror("param is not right!\n");
		exit(-1);
	}
	
	bzero(&s_addr,sizeof(struct sockaddr_in));
	bzero(&c_addr,sizeof(struct sockaddr_in));

	//1. socket
	cfd = socket(AF_INET,SOCK_STREAM,0);

	c_addr.sin_family = AF_INET;
	c_addr.sin_port = htons(atoi(argv[2]));
	inet_pton(AF_INET,argv[1],&c_addr.sin_addr);

	//2. connect
	if(connect(cfd,(struct sockaddr*)&c_addr,sizeof(c_addr)) == -1){
		perror("connect");
		exit(-1);
	}

	if(cfd == -1){
		perror("accept");
		exit(0);
	}
	printf("connect ...\n");
	
	int mark = 0;
	while(1){
			bzero(msg.data,sizeof(msg.data));
			
			if(mark == 0){
				printf(">");
			}
			fgets(msg.data, sizeof(msg.data), stdin);
			msg.data[strcspn(msg.data, "\n")] = 0;
			//gets(msg.data);
			if(strlen(msg.data) == 0){
				if(mark == 1)
				printf(">");
				continue;
			}
			
			mark = 1;
			int ret = msg_handler(msg,cfd);

			if(ret == -1){
				printf("cmd error\n");
				printf(">");
				fflush(stdout);
				continue;
			}
			else if(ret > IFGO){
				printf(">");
				fflush(stdout);
				continue;
			}
			server_msg_handler(ret,msg,cfd);
	}
	

	return 0;
}
